package org.fing.edu.uy.admin.managedBean;


import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.fing.edu.uy.admin.factory.ManagerFactory;
import org.fing.edu.uy.admin.utils.FacesUtils;
import org.fing.edu.uy.esbadp.core.entity.DMRegisteredService;
import org.primefaces.event.FileUploadEvent;

@ViewScoped
@ManagedBean(name="serviceController")
public class ServiceController implements Serializable {

	private static final long serialVersionUID = 1L;
    private List<DMRegisteredService> dataTable;
    private DMRegisteredService selectItem;
    private boolean editMode;
    private boolean newMode;

    private String toCan = "";
    private String fromCan = "";
    
    /** Creates a new instance of ServiceController */
    public ServiceController() {
    }

    @PostConstruct
    public void init(){
        try {
			dataTable = ManagerFactory.getServiceManager().getAllVirtualServices();
		} catch (Exception e) {
			e.printStackTrace();
		}           
    }

    public DMRegisteredService getSelectItem() {
        return selectItem;
    }

    public void setSelectItem(DMRegisteredService selectItem) {    
        this.selectItem = selectItem;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public boolean isNewMode() {
        return newMode;
    }

    public void setNewMode(boolean newMode) {
        this.newMode = newMode;
    }

    public List<DMRegisteredService> getDataTable() {
        return dataTable;
    }

    public void setDataTable(List<DMRegisteredService> dataTable) {
        this.dataTable = dataTable;
    }

    public void save(){
        if(selectItem != null){
            try{
                if(newMode){
                    int id = ManagerFactory.getServiceManager().persistVirtualService(selectItem);
                    FacesUtils.addInfo("The service was persisted with id=" + id); 
                }else{
                	ManagerFactory.getServiceManager().updateService(selectItem);
                	FacesUtils.addInfo("The service was updated"); 
                }
                init();
              
            }catch(Exception ex){
            	ex.printStackTrace();
            }
        }else{	
        	  FacesUtils.addWarn("First select a record."); 
        }
    }

    public void delete(){
        if(selectItem != null){
            try{
            	ManagerFactory.getServiceManager().deleteVirtualService(selectItem.getServiceID());
                init();
                FacesUtils.addInfo("Success"); 
            }catch(Exception ex){
            	ex.printStackTrace();
            }
        }else{
        	FacesUtils.addWarn("First select a record."); 
        }
    }

    public void create(){    
       selectItem = new DMRegisteredService();
    }

    public void reload(){
        init();
    }
    
    public void newSelection(){
    	//cargo las transformaciones si tiene algo    
    	if(selectItem != null){
    		 try{
            	toCan =	ManagerFactory.getServiceManager().getToCanonicalXLST(selectItem.getServiceID());
                fromCan =ManagerFactory.getServiceManager().getFromCanonicalXLST(selectItem.getServiceID());                          
             }catch(Exception ex){
             	ex.printStackTrace();
             }
         }else{
         	toCan = "";
         	fromCan = "";
         }    	
    }
    
    public void handleXSLTToCanonical(FileUploadEvent event) {
        try {
        	ManagerFactory.getServiceManager().addToCanonicalXLST(event.getFile().getInputstream(),selectItem.getServiceID());         
        	FacesUtils.addInfo("Upload successfull");
        } catch (Exception ex) {
        	FacesUtils.addSev("Upload fail");
        }
    }
    
    public void handleXSLTFromCanonical(FileUploadEvent event) {
        try {
        	ManagerFactory.getServiceManager().addFromCanonicalXLST(event.getFile().getInputstream(),selectItem.getServiceID());         
        	FacesUtils.addInfo("Upload successfull");
        } catch (Exception ex) {
        	FacesUtils.addSev("Upload fail");
        }
    }

	public String getFromCan() {
		return fromCan;
	}

	public void setFromCan(String fromCan) {
		this.fromCan = fromCan;
	}

	public String getToCan() {
		return toCan;
	}

	public void setToCan(String toCan) {
		this.toCan = toCan;
	}

}
